//
//  popTransitionAnimation.m
//  练习转场动画
//
//  Created by 苗钧豪 on 16/8/23.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "popTransitionAnimation.h"
#import "TableViewController.h"
#import "ViewController.h"
@implementation popTransitionAnimation
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.5;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    TableViewController *tvc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    ViewController *vc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:tvc.view];
    _cell.oneimgview.hidden = YES;
    
    UIView *snapView = [vc.twoimgview snapshotViewAfterScreenUpdates:NO];
    snapView.frame = CGRectMake(0, 0, 375, 228);
    [containerView addSubview:snapView];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        [containerView layoutIfNeeded];
        CGRect rect = [_cell.oneimgview convertRect:_cell.frame toView:vc.view];
        NSLog(@"----------------------------%@",_cell);
        if (rect.origin.y != 64) {
            rect.origin.y = rect.origin.y / 2;
        }
        snapView.frame = rect;
    } completion:^(BOOL finished) {
        _cell.oneimgview.hidden = NO;
        [snapView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
}
@end
